﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HproseRpcTester.Service;

namespace HproseRpcTester.Utils
{
    //public interface ITestRPCService
    //{
    //    Task<string> TestAsync(string msg);
    //}

    [RPCService]
    public class TestRPCService : RPCServiceBase/*, ITestRPCService*/
    {
        [RPC]
        public async Task<string> TestAsync(string msg)
        {
            return await Task.Run(delegate
            {
                try
                {
                    Console.WriteLine($"{nameof(TestAsync)} Called. Msg：{msg}");
                    return Success();
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{nameof(TestAsync)} Exception：{e.Message}.");
                    return Error(e);
                }
            });
        }

        [RPC]
        public string Test(string msg)
        {
            try
            {
                Console.WriteLine($"{nameof(Test)} Called. Msg：{msg}");
                return Success();
            }
            catch (Exception e)
            {
                Console.WriteLine($"{nameof(Test)} Exception：{e.Message}.");
                return Error(e);
            }
        }

        [RPC]
        public async Task TestWithoutReturnAsync(string msg)
        {
            await Task.Run(delegate
            {
                try
                {
                    Console.WriteLine($"{nameof(TestWithoutReturnAsync)} Called. Msg：{msg}");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{nameof(TestWithoutReturnAsync)} Exception：{e.Message}.");
                }
            });
        }

        [RPC]
        public async void TestWithoutReturnWithAsync(string msg)
        {
            try
            {
                Console.WriteLine($"{nameof(TestWithoutReturnWithAsync)} Called. Msg：{msg}");
            }
            catch (Exception e)
            {
                Console.WriteLine($"{nameof(TestWithoutReturnWithAsync)} Exception：{e.Message}.");
            }
        }

        [RPC]
        public void TestWithoutReturn(string msg)
        {
            try
            {
                Console.WriteLine($"{nameof(TestWithoutReturn)} Called. Msg：{msg}");
            }
            catch (Exception e)
            {
                Console.WriteLine($"{nameof(TestWithoutReturn)} Exception：{e.Message}.");
            }
        }

        [RPC]
        public void TestWithoutReturnWithTask(string msg)
        {
            Task.Run(() =>
            {
                try
                {
                    Console.WriteLine($"{nameof(TestWithoutReturnWithTask)} Called. Msg：{msg}");
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{nameof(TestWithoutReturnWithTask)} Exception：{e.Message}.");
                }
            });
        }
    }
}
